package meditation;


import javax.media.*;

import java.awt.*;
import java.io.*;
 

public class Media 
{
    public static int SCHLAGWARTEZEIT=800;
    public static int MilliSec_PER_MINUTE=1000*60;
    
    public static void main(String[] args) throws NoPlayerException, IOException, ClassNotFoundException, InterruptedException {
		
		zingGlass(1,800);
		Thread.sleep(1000*60*1);
		zingGlass(3,800);		
		Thread.sleep(1000*60*15);
		zingGlass(2,800);
		Thread.sleep(1000*60*10);
		zingGlass(1,800);
		Thread.sleep(1000*60*5);
		zingGlass(3,800);

	}
        
        public static void sesshin (
                 int zeit_zum_Hinsetzen,
                 int zzHSchlaege,
                 int zeit_bis_zur_Mitte,
                 int zbzMSchlaege,
                 int zeit_bis_kurz_vor_Ende,
                 int zbkvESchlaege,
                 int zeit_bis_Ende,
                 int zbESchlaege
                )
                throws NoPlayerException, IOException, ClassNotFoundException, InterruptedException 
                 {            
                     
                zingGlass(1,SCHLAGWARTEZEIT);
		Thread.sleep(MilliSec_PER_MINUTE*zeit_zum_Hinsetzen);
		zingGlass(zzHSchlaege,SCHLAGWARTEZEIT);		
		Thread.sleep(MilliSec_PER_MINUTE*zeit_bis_zur_Mitte);
		zingGlass(zbzMSchlaege,SCHLAGWARTEZEIT);
		Thread.sleep(MilliSec_PER_MINUTE*zeit_bis_kurz_vor_Ende);
		zingGlass(zbkvESchlaege,SCHLAGWARTEZEIT);
		Thread.sleep(MilliSec_PER_MINUTE*zeit_bis_Ende);
		zingGlass(zbESchlaege,SCHLAGWARTEZEIT);
            
            
        }
	
	public static void zingGlass(int times, int waitMs) throws NoPlayerException, IOException, ClassNotFoundException, InterruptedException {
		File soundFile = new File("C:\\Users\\hassler\\Desktop\\anvil-wood03.wav");		
		
		for (int i=0; i<times;i++) {
			playMusic (soundFile);
			Thread.sleep(waitMs);
		}
	}
	
	private static void checkJmf() throws ClassNotFoundException
    {
        try{
            Class c = Class.forName("javax.media.Manager");
        }catch (ClassNotFoundException e){
            ClassNotFoundException cnfe = new ClassNotFoundException ("JMF nicht installiert!", e);
            throw cnfe;
        }
    }
    private Media (Component visual, Component controlPanel, Player player)
    {
        v = visual;
        cp = controlPanel;
        p = player;
    }
    private Component v;
    private Component cp;
    private Player p;
    /**
     *@return Das visualComponent des Players, sofern vorhanden, ansonsten null
     *@see Player#getVisualComponent
     */
    public Component getVisualComponent()
    {
        return v;
    }
    /**
     *@return Das controlPanelComponent des Players
     *@see Player#getControlPanelComponent
     */
    public Component getControlPanelComponent()
    {
        return cp;
    }
    /**
     *@return Der Player
     *@see javax.media.Player
     */
    public Player getPlayer()
    {
        return p;
    }
    /**
     *Spielt mit dem JMF die Musik in der Datei f ab.
     *@return Ein Media-Objekt, dass das ControlPanelComponent und den Player kapselt
     *@exception IOException Wenn die Datei nicht existiert
     *@exception ClassNotFoundException Wenn kein JMF installiert ist
     *@exception NoPlayerException Wenn kein Player zu der Datei gefunden wird.
     */
    public static Media playMusic (File f) throws IOException, ClassNotFoundException, NoPlayerException
    {
        checkJmf();
        final Player player = Manager.createPlayer (f.toURL());
        final Object LOCK = new Object();
        player.addControllerListener (new ControllerListener(){
            public synchronized void controllerUpdate(ControllerEvent event) { 
                if (event instanceof RealizeCompleteEvent) { 
                    synchronized (LOCK){
                        LOCK.notifyAll();
                    }
                } 
            } 
        });
        synchronized (LOCK){
            player.start();
            try{
                LOCK.wait();
            }catch (InterruptedException ie){
                Thread.currentThread().interrupt();
            }
        }
        return new Media (null, player.getControlPanelComponent(), player);
    }
    /**
     *Spielt mit dem JMF das Video in der Datei f ab.
     *@return Ein Media-Objekt, dass das ControlPanelComponent, das VisualComponent und den Player kapselt
     *@exception IOException Wenn die Datei nicht existiert
     *@exception ClassNotFoundException Wenn kein JMF installiert ist
     *@exception NoPlayerException Wenn kein Player zu der Datei gefunden wird.
     */
    public static Media playVideo (File f) throws IOException, ClassNotFoundException, NoPlayerException
    {
        checkJmf();
        final Player player = Manager.createPlayer (f.toURL());
        final Object LOCK = new Object();
        player.addControllerListener (new ControllerListener(){
            public synchronized void controllerUpdate(ControllerEvent event) { 
                if (event instanceof RealizeCompleteEvent) { 
                    synchronized (LOCK){
                        LOCK.notifyAll();
                    }
                } 
            } 
        });
        synchronized (LOCK){
            player.start();
            try{
                LOCK.wait();
            }catch (InterruptedException ie){
                Thread.currentThread().interrupt();
            }
        }
        return new Media (player.getVisualComponent(), player.getControlPanelComponent(), player);
    }
    /**
     *Blockiert bis zu einem StopEvent des Players, das kein Restarting- oder StopByRequestEvent ist.
     *@see javax.media.StopEvent
     */
    public static void waitFor (Player player)
    {
        final Object LOCK = new Object();
        player.addControllerListener (new ControllerListener(){
            public synchronized void controllerUpdate(ControllerEvent event) { 
                if (event instanceof javax.media.StopEvent && !(event instanceof RestartingEvent) && !(event instanceof StopByRequestEvent)) { 
                    synchronized (LOCK){
                        LOCK.notifyAll();
                    }
                } 
            } 
        });
        synchronized (LOCK){
            try{
                LOCK.wait();
            }catch (InterruptedException ie){
                Thread.currentThread().interrupt();
            }
        }
    }
}